import type { IDispatchStatus } from "@/types/provideFuncs";
import { ElMessage } from "element-plus";

/**
 * 编辑组件通知更改状态
 * @param store 当前编辑器store
 * @param currentComJsonSchema 当前选中的组件的jsonSchema
 * @param statusKey 状态的key
 * @param value 状态的value
 */
export const dispatchStatus: IDispatchStatus= (
  store,
  currentComJsonSchemaStatus,
  statusKey,
  value,
) => {
  // console.log(statusKey, value);
  switch (statusKey) {
    case "title":
    case "desc":
      if (typeof value !== "string") {
        return console.error(
          'Invalid payload type for "title or desc". Expected string.',
        );
      }
      store.setTextStatus(
        currentComJsonSchemaStatus[statusKey],
        value,
      );
      break;
    case "options":
      console.log("设置图片链接", value);
      if (typeof value === "number") {
        // - 删除
        const result = store.deleteOptionStatusItem(
          currentComJsonSchemaStatus[statusKey]!,
          value,
        );
        if (result) {
          ElMessage.success("删除成功");
        } else {
          ElMessage.error("至少保留两个选项");
        }
      } else if (typeof value === "object") {
        // - 设置图片链接
        store.setPicSelectOptionItem(
          currentComJsonSchemaStatus[statusKey]!,
          value.index,
          value.link,
        );
      } else {
        // - 添加选项
        store.addOptionStatusItem(
          currentComJsonSchemaStatus[statusKey]!,
        );
      }
      break;
    case "position":
      if (typeof value !== "number") {
        return console.error(
          'Invalid payload type for "position". Expected number.',
        );
      }
      store.setStatusIndex(
        currentComJsonSchemaStatus[statusKey],
        value,
      );
      break;
    case "titleSize":
    case "descSize":
      if (typeof value !== "number") {
        return console.error(
          'Invalid payload type for "titleSize or descSize". Expected number.',
        );
      }
      store.setStatusIndex(
        currentComJsonSchemaStatus[statusKey],
        value,
      );
      break;
    case "titleWeight":
    case "descWeight":
      if (typeof value !== "number") {
        return console.error(
          'Invalid payload type for "titleWeight or descWeight". Expected number.',
        );
      }
      store.setStatusIndex(
        currentComJsonSchemaStatus[statusKey],
        value,
      );
      break;
    case "titleItatic":
    case "descItatic":
      if (typeof value !== "number") {
        return console.error(
          'Invalid payload type for "titleItatic or descItatic". Expected number.',
        );
      }
      store.setStatusIndex(
        currentComJsonSchemaStatus[statusKey],
        value,
      );
      break;
    case "titleColor":
    case "descColor":
      if (typeof value !== "string") {
        return console.error(
          'Invalid payload type for "titleColor or descColor". Expected string.',
        );
      }
      store.setTextStatus(
        currentComJsonSchemaStatus[statusKey],
        value,
      );
      break;
    // + 备注类型
    case "remarkType":
      if (typeof value !== "number") {
        return console.error(
          'Invalid payload type for "type". Expected number.',
        );
      }
      // - 切换编辑面板的显示状态
      store.changeEditorVisible(
        currentComJsonSchemaStatus,
        value,
      );
      // - 设置type的选中项索引（切换类型：标题 or 描述）
      store.setStatusIndex(
        currentComJsonSchemaStatus[statusKey]!,
        value,
      );
      break;
    // + 输入类型
    case "inputType":
      if (typeof value !== "number") {
        return console.error(
          'Invalid payload type for "inputType". Expected number.',
        );
      }
      store.setStatusIndex(
        currentComJsonSchemaStatus[statusKey]!,
        value,
      );
      break;
    // + 宽度
    case "width":
      if (typeof value !== "string") {
        return console.error(
          'Invalid payload type for "width". Expected string.',
        );
      }
      store.setTextStatus(
        currentComJsonSchemaStatus[statusKey]!,
        value,
      );
      break;
    case "rateSetting":
      if (typeof value === "boolean") {
        store.setRateIsUse(currentComJsonSchemaStatus, value);
      }
      break;
    case "dateType":
      if (typeof value !== "number") {
        return console.error(
          'Invalid payload type for "dateType". Expected number.',
        );
      }
      store.setStatusIndex(
        currentComJsonSchemaStatus[statusKey]!,
        value,
      );
      break;
  }
};
